package com.googlecode.garbagecan.simonstudy;

import org.javasimon.CallbackSkeleton;
import org.javasimon.SimonManager;
import org.javasimon.Split;
import org.javasimon.Stopwatch;
import org.javasimon.utils.SimonUtils;

public class CallbackExample {
	public static void main(String[] args) throws Exception {
		SimonManager.callback().addCallback(new CallbackSkeleton() {
			public void stopwatchStart(Split split) {
				System.out.println("Stopwatch " + split.getStopwatch().getName()
					+ " has just been started.");
			}

			public void stopwatchStop(Split split) {
				System.out.println("Stopwatch " + split.getStopwatch().getName()
					+ " has just been stopped (" + SimonUtils.presentNanoTime(split.runningFor()) + ").");
			}
		});

		Stopwatch sw = SimonManager.getStopwatch(CallbackExample.class.getName());
		sw.start().stop();
		
		for (int i = 0; i < 100; i++) {
			Split split = sw.start();
			Thread.sleep((long) (Math.random() * 1000L));
			split.stop();
		}
		
		System.out.println(sw);
	}
}
